﻿/// <reference path="Scripts/jquery-1.4.2-vsdoc.js" />
/// <reference path="Scripts/MicrosoftAjax.js" />

$(document).ready(function() {
listTableTrOver();
    
    //初始化日期控件
    $('.dateinput').each(function() {
        $(this).date_input();
        if ($(this).val().length > 0) {
            $(this).val($(this).val().split(" ")[0]);
        }
    });
    //全选
    CheckAll();
    $('.staffChooser').each(function() {
        $(this).staffChooser(true);
    });
    $('.deptchooser').each(function() {
        $(this).departmentChooser(true);
    });
    //Ajax提示
    $(this).ajaxStart(function() {
        if (typeof (displayLoading) != 'undefined' && displayLoading) {
            ajaxLoading.start("正在请求数据...");
        }
    });
    $(this).ajaxComplete(function(event, request, settings) {
        if (typeof (displayLoading) != 'undefined' && displayLoading) {
            ajaxLoading.complete();
        }
    });
    initConditionDisplay();
});

/********************************************/
/* Ajax加载提示类                      */
/* 2010-10-19                               */
/********************************************/

var ajaxLoading = {};
//开始
ajaxLoading.start = function(message) {
    if ($("#infoLoadingContainer") != null)
        $("#infoLoadingContainer").remove();
    var html = $("<div id='infoLoadingContainer'><div>" +
                    "<span class='text'>" + message + "</span>" +
                    "</div></div>");
    $("body").append(html);
}
//请求完成
ajaxLoading.complete = function() {
    if ($("#infoLoadingContainer") != null)
        $("#infoLoadingContainer").remove();
}
/********************************************/
/*列表样式                                  */
/* 2010-10-19                               */
/********************************************/
function listTableTrOver() {
    $(".tblData tr").not(".lt_head").hover(function() {
        $(this).addClass("over");
    }, function() {
        $(this).removeClass("over");
    });
    $(".list-table-line tr").not(".lt_head").hover(function() {
        $(this).addClass("over");
    }, function() {
        $(this).removeClass("over");
    });
    $(".list-table-line tr:not(.timespan-table tr):even").not(".lt_head").addClass("alt");
}


// 消息提示的类
var InfoTip = {};
InfoTip._timer = undefined;
// 显示消息的类型
InfoTip.type = [
    "提示",  
    "成功",
    "警告",
    "错误"
];
// 显示消息的类型
InfoTip.img = [
    "info",
    "success",    
    "warning",
    "delete"
];
// 显示消息 CreateBy  ChangYuLin
InfoTip.showMessage = function(message, typeID, timeout) {

    if (timeout == undefined)
        timeout = 2500;

    if (InfoTip._timer) {
        clearTimeout(InfoTip._timer);
        InfoTip._timer = undefined;
    }
    $(".info").remove();
    var type = InfoTip.type[typeID];
    var img = InfoTip.img[typeID];
    var note = $("<div class='info'><div class='info_title'><span class='close'></span>" + type + "</div><div class='conter_" + typeID + "' style='min-height:57px;'>" + message + "</div>");
    $(document.body).append(note);
    $(".close", note).show().click(function() {
        note.animate({ top: 102 }, "fast", null, function() { note.hide(); });
    });


    note.animate({ top: 122 }, "normal");

    if (timeout > 0) {
        InfoTip._timer = setTimeout(function() {
            note.animate({ top: 102 }, "slow", null, function() { note.hide(); });
            InfoTip._timer = undefined;
        }, timeout, undefined);
    }

    var left_pos = (($(document).width() - $(note).width()) / 2);
    note.css("left", left_pos + "px");
    note.show();
};




/********************************************/
//添加关注
/********************************************/
function newFocus(rid, sid, callback) {
    if (!rid && !sid) {
        alert("参数错误,站点id和路线id 不能同时为空,Code:-1");
        return;
    } 
    var path = appRootPath + "Focus/NewFocus";
     //提交
    $.ajax({
        type: "POST",
        url: path,
        data: { "stationId": sid,"routeId":rid },
        success: function(msg) {
            if (msg.StatuCode != -2) {
                InfoTip.showMessage("该站点(路线)已被添加到关注站点(路线)！", 1);
                if (typeof (callback) != "undefined") {
                    callback(1);
                }
            }
            else {
                InfoTip.showMessage("添加失败，相关数据不存在！", 4);
            }
        }
    });
}

/********************************************/
//发送路线信息到手机
/********************************************/
function sendToMyPhone(gid) {
    var path = appRootPath + "Route/SendToMyPhone";
    $.ajax({
        type: "POST",
        url: path,
        data: { "gid": gid },
        success: function(msg) {
            if (msg.StatuCode == 0) {
                InfoTip.showMessage("已将路线信息发送到手机，请注意查收！", 1);
            }
            else {
                InfoTip.showMessage("发送失败，<br/>错误信息：" + msg.Error, 2, 6000);
            }

        }
    });
}

/********************************************/
/*全选                                      */
/*                                          */
/********************************************/
function CheckAll() {
    $("#check_all").click(function() {
        $("#gvList :checkbox").attr("checked", $(this).attr("checked"));
    })
}
//验证邮箱格式
function checkEmail(s) {
    var validExp = /^[\w-]+@[\w-]+[\.\w-]+$/;
    return validExp.test(s);
}
//检查是否输入HTML字符
function chekHtml() {
    var isSpecialChar = true;
    var re = /[\*\?#&<>]+/;
    $("input[type='text'],textarea").each(function() {
        if (re.test($(this).val())) {
            InfoTip.showMessage("不允许输入\*?#&<>等特殊字符！", 2);
            isSpecialChar = false;
            $(this).addClass("input-validation-error");
            $(this).focus();
        }
        else {
            $(this).removeClass("input-validation-error");
        }
    });
    return isSpecialChar;
}

/********************************************/
/*时间格式校验                 */
/********************************************/
function checkDataRangeFormat() {
    $(".dateinput").blur(function() {
        var idStr = $(this).attr("id");
        var dtStart;
        var dtEnd;

        var regEnd = /^[\w]+End$/;
        if (idStr.match(regEnd) == null) {
            dtStart = $("#" + idStr);
            dtEnd = $("#" + idStr + "End");
        }
        else {
            dtEnd = $("#" + idStr);
            dtStart = $("#" + idStr.substring(0, idStr.length - 3));
        }
        if (dtEnd.val() == '' || dtStart.val() == '' || dtEnd.val() == 'undefined' || dtStart.val() == 'undefined')
            return;
        if (compareDate(dtStart.val(), dtEnd.val())) {
            $(this).val("");
            InfoTip.showMessage("开始时间不能晚于结束时间！", "note");

            return true;
        }
    });
}
/********************************************/
// 时间比较的方法，如果d1时间比d2时间大，则返回true   
/********************************************/
function compareDate(d1, d2) {
    return Date.parse(d1.replace(/-/g, "/")) > Date.parse(d2.replace(/-/g, "/"))
}
function loadSubnav(list) {
    var subnav = $("div.subnav .c");
    subnav.empty();
    $(list).each(function(i, item) {
        subnav.append("<a href='" + item.href + "' class=" + (item.css ? item.css : "unselect") + ">" + item.text + "</a>");
    });
}

/*获取长度函数-区分中英文*/
function getBytesLength(str) {
    var re = /[\x00-\xff]/g;
    var len = str.length;
    var array = str.match(re);
    if (array == null) {
        array = "";
    }
    return len * 2 - array.length;
}
//trim()去掉字串两边的空格
function trim(string) {
    var trimResult = string;
    //去掉半角空格
    trimResult = trimResult.replace(/(^\s*)|(\s*$)/g, "");
    //去掉全角角空格
    trimResult = trimResult.replace(/(^　*)|(　*$)/g, "");

    return trimResult;
}

// 数字全角→半角
function NumberZen2Han(str) {
    return str.replace(/[０-９]/g, function(str) { return String.fromCharCode(str.charCodeAt(0) - 65248); });
}

/* -------------------------------------------- */
/* 2010-03-31									*/
/* spectorye									*/
/* 实现弹出模态窗口								*/
/* 需要jquery支持								*/
/* -------------------------------------------- */
function GetCover() {
    var divCover = $("#norepeat_cover");
    if (divCover == null || divCover.length <= 0) {
        divCover = $('<div id="norepeat_cover"><iframe frameborder="0">&nbsp;</iframe></div>');//
        divCover.appendTo("body");
    }
    return divCover;
}
function GetModel() {
    var divModel = $("#norepeat_model");
    if (divModel == null || divModel.length <= 0) {
        divModel = $('<div id="norepeat_model"><div class="modelheader">'
			+ '<div class="title"></div>'
			+ '<div class="link">'
			+ '<a href="javascript:showModel();">关闭</a>'
			+ '</div>'
			+ '</div><iframe frameborder="0" scrolling="no">&nbsp;</iframe></div>');
        divModel.appendTo("body");
    }
    return divModel;
}
function window_resize() {
    var divCover = GetCover();
    if (divCover == null || divCover.length <= 0) {
        return;
    }
    var h = Math.max($(window).height(), $(document).height());
    var w = Math.max($(window).width(), $(document).width());
    divCover.width(w - 2);
    divCover.height(h - 2);
    //
    window_scroll();
}
function window_scroll() {
    var divCover = GetCover();
    var divModel = GetModel("");
    if (divCover != null && divModel != null) {
        var scrollTop = $(window).scrollTop();
        var scrollLeft = $(window).scrollLeft();
        //设置浮动层
        divModel.css("top", ($(window).height() - divModel.height()) / 2 + scrollTop);
        divModel.css("left", ($(window).width() - divModel.width()) / 2 + scrollLeft);
    }
}
function showModel(url, title, w, h) {
    if (url && url.length > 0) {
        var nr_cover = GetCover();
        var nr_model = GetModel();
        if (h && h > 0) {
            nr_model.height(h);
        }
        if (w && w > 0) {
            nr_model.width(w);
        }
        if (title && title.length > 0) {
            nr_model.find(".title").text(title);
        }
        var iframe = nr_model.children("iframe");
        if (iframe.length > 0) {
            iframe.attr("src", url);
            iframe.height(h - 28);
            iframe.width(w - 8);
        }
        window_resize();
        nr_cover.show();
        nr_model.show();
        $(window).resize(window_resize);
        $(window).scroll(window_scroll);
    }
    else {
        var nr_cover = GetCover();
        var nr_model = GetModel();
        //nr_cover.hide();
        //nr_model.hide();
        nr_cover.remove();
        nr_model.remove();
        $(window).die("resize", window_resize);
        $(window).die("scroll", window_scroll);
    }
}
/********************************************/
//获取时间戳字符串，用以在请求中避免Cache
/********************************************/
function getTimeTick() {
    var myDate = new Date();
    return myDate.getDate() + '-' +
                    myDate.getHours() + '-' +
                    myDate.getMinutes() + '-' +
                    myDate.getSeconds() + '-' +
                    myDate.getMilliseconds();
}


//控制键盘的按键，只能输入数字
function inputNumber(e) {
    var keynum;
    var keychar;
    var numcheck;
    if (window.event) // IE
    {
        keynum = e.keyCode
    } else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }
    keychar = String.fromCharCode(keynum);
    return !isNaN(keychar);
}
//初始化条件显示状态
function initConditionDisplay() {
    if (!$("#condition_hidden"))
        return;
    var hidVal = $("#condition_hidden").val();
    if (hidVal) {
        switchToHidden(false);
        isShow = false;
    }
    else{
        switchToDisplay(false);
        isShow = true;
    }
    $("#btn_query").bind("click", condtionBtnHandler);
}
var isShow;

function condtionBtnHandler() {
    if (!isShow) {        
        switchToDisplay(false);
    }
    else {
        switchToHidden(false);
    }
    isShow = !isShow;
}

function switchToDisplay(effet) {
    $("#btn_query").val("显示查询");
    $("#condition").hide();
    $("#condition_hidden").val("");
}
function switchToHidden(effet) {
    $("#btn_query").val("隐藏查询"); 
    $("#condition").show();
    $("#condition_hidden").val("display");
}

/***********************地图图标ID 开始*********************/
//用于初始化地图图标的A-Z
function MapIconAZ(url, pre) {
    var me = this;
    me.url = url;
    me.pre = pre;
    me.idx = -1;
    me.icons = me.initIcons();
}

MapIconAZ.prototype.initIcons = function() {
    var me = this;
    var arricon = [];
    var a = "A", z = 'Z';
    var ac = a.charCodeAt(0);
    var zc = z.charCodeAt(0);
    var i = 0;
    var c = '';
    for (i = ac; i <= zc; i++) {
        c = unescape('%' + i.toString(16));
        var mi = new google.maps.MarkerImage(
              me.url + me.pre + c + '.png',
              new google.maps.Size(20, 34),
              new google.maps.Point(0, 0),
              new google.maps.Point(10, 34),
              new google.maps.Size(20, 34));
        arricon.push(mi);
    }
    me.idx = -1;
    return arricon;
};

MapIconAZ.prototype.reset = function() {
    this.idx = -1;
};
MapIconAZ.prototype.next = function() {
    var me = this;
    var a = "A", z = 'Z';
    var ac = a.charCodeAt(0);
    var zc = z.charCodeAt(0);
    if (me.idx >= (zc - ac))
        me.idx = 0;
    else
        me.idx++;
    return me.icons[me.idx];
};
/***********************地图图标ID 结束*********************/

/***********************保存确认对话款 BEGIN*********************/
var isFormChanged = false;
function initFormState() {
    $("input,textarea").change(function() { isFormChanged = true; });
    $(".oplink").click(function() { isFormChanged = true; });
    window.onbeforeunload = function() {
        if (isFormChanged) {
            var confirmStr =    "-----------------------------------------------------\r\n\r\n" +
                                "页面数据未保存，是否确认离开该页面？\r\n\r\n" +
                                "-----------------------------------------------------";
            return confirmStr; 
        }
        return ;
    };
}
function resetFormState() {
    isFormChanged = false;
}

/***********************保存确认对话款ID END*********************/